package com.ljyanimo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljyanimo.constant.AnimoConstant;
import com.ljyanimo.constant.ServerResponseConstant;
import com.ljyanimo.entity.SysUser;
import com.ljyanimo.enums.ActivationEnum;
import com.ljyanimo.response.ServerResponse;
import com.ljyanimo.search.UserSearch;
import com.ljyanimo.service.ISysUserService;
import com.ljyanimo.table.UserTable;
import com.ljyanimo.utils.PasswordHelper;
import com.ljyanimo.vo.SaveUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.rmi.server.Activation;

import java.util.ArrayList;
import java.util.List;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

/**
 * <p>
 *  用户控制器
 * </p>
 *
 * @author
 * @since 2020-09-30
 */
@Controller
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 跳转后台用户列表
     * @return
     */
    @GetMapping("/listPage")
    public String listPage(){
        return "user/list";
    }


    @PostMapping("/list")
    @ResponseBody
    public ServerResponse list(@RequestBody UserSearch userSearch){
        IPage<SysUser> iPage = new Page<>(userSearch.getPage(),userSearch.getLimit());

        QueryWrapper queryWrapper = new QueryWrapper();

        IPage<SysUser> page = iSysUserService.page(iPage, queryWrapper);

        IPage<UserTable> userTableIPage = new Page<>();

        BeanUtils.copyProperties(page,userTableIPage);

        /**
         * 这里是针对进行一些格式化的地方 比如不需要的字段或者需要转换的字段
         */
        List<UserTable> list;
        list = page.getRecords().stream().map(e->{
            UserTable userTable = new UserTable();
            BeanUtils.copyProperties(e,userTable);
            userTable.setActive(ActivationEnum.getOneByCode(e.getActive()).getValue());
            return userTable;
        }).collect(Collectors.toList());

        userTableIPage.setRecords(list);

        return ServerResponse.createBySuccess(userTableIPage);
    }

    /**
     * 用户添加操作
     * @param saveUserVo
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ServerResponse save(@RequestBody SaveUserVo saveUserVo){
        // TODO 需要校验参数

        // 判断账号是否存在
        String account = saveUserVo.getAccount();
        SysUser sysUser = iSysUserService.getOne(new QueryWrapper<SysUser>().eq("account", account));
        if(sysUser != null){
            return ServerResponse.createByError(ServerResponseConstant.ACCOUNTEXIST);
        }

        checkUserParams(saveUserVo);

        String salt = PasswordHelper.createSalt();
        // 设置加密密码
        saveUserVo.setPassword(PasswordHelper.createCredentials(saveUserVo.getPassword(),salt));


        SysUser saveUser = new SysUser();
        BeanUtils.copyProperties(saveUserVo,saveUser);
        saveUser.setSalt(salt);
        saveUser.setCreateTime(LocalDateTime.now());
        saveUser.setUpdateTime(LocalDateTime.now());
        iSysUserService.save(saveUser);
        return ServerResponse.createBySuccess(ServerResponseConstant.ADDSUCCESS);
    }

    private void checkUserParams(SaveUserVo saveUserVo){
        // 检查nickName是否为空，如果为空则使用账号替代
        if(StringUtils.isEmpty(saveUserVo.getNickName())){
            saveUserVo.setNickName(saveUserVo.getAccount());
        }
        // 检查密码是否为空
        if(StringUtils.isEmpty(saveUserVo.getPassword())){
            saveUserVo.setPassword(AnimoConstant.DEFAULTPASSWORD);
        }
    }



}
